/**
 * Created by zhu on 2016/1/30.
 */
$(function () {

    $.get(contextPath + "/collection", function (data) {
        myCollection(data);
    });


});

function myCollection(data) {
    data = jQuery.parseJSON(data);
    var tempDiv = $('#collection_list');
    $.each(data.list, function (i, item) {
        var temp = ' <div class="list-block first_list">' +
            ' <ul>' +
            '     <li class="item-content ">' +
            '  <div class="item-media"><i class="icon icon-browser"></i></div>' +
            '  <div class="item-inner">' +
            '<div class="item-title">' + item.users.shopName + '</div>' +
            '<div class="item-after">&emsp;|&emsp;<a href="#" onclick="productDetails(' + item.id + ')">购买</a></div>' +
            ' </div>' +
            ' </li>' +
            ' <li class="item-content">' +
            ' <div style="width: 100%;height: 100px"><div  style="width: 10%;float: left;height: 100px;">' +
            '<input type="hidden" value="' + item.id + '"/>' +
            '<div class="radius" style="margin-top: 35px"></div></div>' +
            ' <div class="item-goods-img">' +
            '<img src="' + contextPath + item.index_url + '">' +
            ' </div>' +
            ' <div class="item-goods-text">' +
            ' <div class = "introduce">' + item.name + '</div>' +
            ' <div class = "price">' +
            ' <div class = "this-price">' + item.price + '</div>' +
            '<div class = "this-num"><a class="reduce">-</a>&emsp;<span class="myNumber">1</span>&emsp;<a class="add">+</a></div>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</li>' +
            '</ul>' +
            '</div>';
        tempDiv.append(temp);
    });

    var title = $('#title');
    var price = 0;
    var number = 0;
    var allPrice = $('#allPrice');
    var allNumber = $('#allNumber');
    $(".radius").click(function () {
        if (this.hasChildNodes()) {
            $(this).empty();
        } else {
            $(this).append('<div id = "innerDIV" ></div>');
        }
        var i = $(".radius").length - 1;
        number = 0;
        for (var j = 0; j <= i; j++) {
            if ($(".radius")[j].hasChildNodes()) {
                number = number + 1;
            }
        }
        var mynumber = $('.myNumber').length - 1;
        price = 0;
        for(var i = 0;i<=mynumber;i++){
            if($('.radius')[i].hasChildNodes()){
                var tempPrice = $($('.myNumber')[i]).parent().parent().children()[0].innerHTML;
                var tempNumber =$($('.myNumber')[i]).text();
                price = price + tempNumber*tempPrice;
            }
        }

        allPrice.text("共" + price + "元");
        allNumber.text("共" + number + "件");
    });

    $("#selectAll").click(function () {

        var i = $(".radius").length - 1;

        if (this.hasChildNodes()) {
            $(this).empty();
            for (var j = 0; j <= i; j++) {

                if ($(".radius")[j].hasChildNodes()) {
                    $($(".radius")[j]).empty();
                }
            }
        } else {
            $(this).append('<div id = "innerDIV" ></div>');
            for (var j = 0; j <= i; j++) {

                if (!$(".radius")[j].hasChildNodes()) {
                    $($(".radius")[j]).append('<div id = "innerDIV" ></div>');
                }
            }
        }

        number = 0;
        for (var j = 0; j <= i; j++) {
            if ($(".radius")[j].hasChildNodes()) {
                number = number + 1;
            }
        }
        var mynumber = $('.myNumber').length - 1;
        price = 0;
        for(var i = 0;i<=mynumber;i++){
            if($('.radius')[i].hasChildNodes()){
                var tempPrice = $($('.myNumber')[i]).parent().parent().children()[0].innerHTML;
                var tempNumber =$($('.myNumber')[i]).text();
                price = price + tempNumber*tempPrice;
            }
        }
        allNumber.text("共" + number + "件");
        allPrice.text("共" + price + "元");
    });

    $('.add').click(function () {
        $($(this)).parent().children('span').text(Number($($(this)).parent().children('span').text()) + 1);
        var mynumber = $('.myNumber').length - 1;
        price = 0;
        for(var i = 0;i<=mynumber;i++){
            if($('.radius')[i].hasChildNodes()){
                var tempPrice = $($('.myNumber')[i]).parent().parent().children()[0].innerHTML;
                var tempNumber =$($('.myNumber')[i]).text();
                price = price + tempNumber*tempPrice;
            }
        }
        allPrice.text("共" + price + "元");
    });

    $('.reduce').click(function () {
        if ($(this).parent().children('span').text() > 1) {
            $($(this)).parent().children('span').text(Number($($(this)).parent().children('span').text()) - 1);
            var mynumber = $('.myNumber').length - 1;
            price = 0;
            for(var i = 0;i<=mynumber;i++){
                if($('.radius')[i].hasChildNodes()){
                var tempPrice = $($('.myNumber')[i]).parent().parent().children()[0].innerHTML;
                var tempNumber =$($('.myNumber')[i]).text();
                price = price + tempNumber*tempPrice;
                }
            }
        }
        allPrice.text("共" + price + "元");
    });

    $('#buyBtn').click(function () {
        var form = $('<form></form>');
        form.attr('action',contextPath+"/cart/confirm");
        form.attr('method','post');
        form.attr('target',"_self");
        var  all_price = '<input type="text" name="allPrice" value="'+price+'"/>';
        var  all_number = '<input type="text" name="allNumber" value="'+number+'"/>';
        var k = 0;
        var i = $(".radius").length - 1;
        for (var j = 0; j <= i; j++) {
            if ($(".radius")[j].hasChildNodes()) {
                var  tempId = '<input type="text" name="id'+k+'" value="'+$($('.radius')[j]).parent().children('input').val()+'"/>';
                var tempNumber = '<input type="text" name="number'+k+'" value="'+$($('.myNumber')[j]).text()+'"/>';
                form.append(tempId);
                form.append(tempNumber);
                k++;
            }
        }
        form.append('<input type="text" name="flag" value="'+k+'"/>');
        form.append(all_price);
        form.append(all_number);
        form.submit();
    });

}